﻿using System;
using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;
using Plumber.Accounting.ModuleData;
using Plumber.Sales.ModuleData;
using ApplicationLibrary.CounterManager;
using System.Windows.Forms;

namespace Plumber.Sales.Forms
{

    public partial class PrintAllSales : ApplicationLibrary.DBManager.BatchForm
    {
        private const string PRINT_INVOICE = "PRINT_INVOICE";

        public PrintAllSales()
        {
            InitializeComponent();
        }

        protected override void OnAttachData()
        {
        }

        protected override void OnBatchExecute()
        {
            SalesInvoice si = new SalesInvoice();
            si.ShowDialog(false);
            PrintInfo pInfo = new PrintInfo(PRINT_INVOICE, PrintType.Letter);

            QueryBuilder qb = new QueryBuilder(true);
            qb.Clear();
            qb.AddSelectAll(PL_SalesInvoices.Name);
            qb.AddFrom(PL_SalesInvoices.Name);
            string filter = string.Format("YEAR(ReleaseDate) = {0} and MONTH(ReleaseDate) = {1}", nudYear.Value, nudMonth.Value);
            qb.AddFilter(filter);
            qb.AddOrder(PL_SalesInvoices.InvoiceNo);

            using (SqlABCommand scc = new SqlABCommand(base.providerType, GlobalInfo.SqlConnection))
            {
                scc.CommandText = qb.Query;
                SqlABDataReader dr = scc.ExecuteReader();
                while (dr.Read())
                {
                    RadarSaleInvoicesParam rdr = new RadarSaleInvoicesParam(dr.GetValue<string>(PL_SalesInvoices.InvoiceNo));
                    si.FindRecord(rdr);
                    si.OnPrintEvent(pInfo);
                }
            }
            si.Close();
        }
    }
}